package com.rpc.common.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertiesUtil {
    private String hostAddress ="";
    private Integer port =null;
    private String serverAddress="";
    private Integer serverPort=null;
    private Properties properties;
    private String filePath;

    private static PropertiesUtil util=new PropertiesUtil();

    private PropertiesUtil() {

    }

    public void setPropertiesFilePath(String filePath){
        // 使用ClassLoader加载properties配置文件生成对应的输入流
        InputStream in = PropertiesUtil.class.getClassLoader().getResourceAsStream(filePath);
        // 使用properties对象加载输入流
        properties=new Properties();

        try {
            properties.load(in);
            this.filePath=filePath;
        } catch (IOException e) {
            System.out.println("加载配置文件失败");
            e.printStackTrace();
        }
    }

    public static PropertiesUtil getPropertiesUtil(String filePath){
        return util;
    }

    public String getHostAddress(){
        if(hostAddress.isEmpty()){
            hostAddress =properties.getProperty("hostAddress");
        }
        return hostAddress;
    }

    public int getPort(){
        if(port ==null){
            port =Integer.valueOf(properties.getProperty("port"));
        }
        return port;
    }

    public String getServerAddress() {
        if(serverAddress.isEmpty()){
            serverAddress=properties.getProperty("serverAddress");
        }
        return serverAddress;
    }

    public Integer getServerPort() {
        if(serverPort==null){
            serverPort=Integer.valueOf(properties.getProperty("serverPort"));
        }
        return serverPort;
    }
}
